﻿from django.db import models
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User
import random
import string

# Create your models here.

class Article(models.Model): #文章

    id = models.IntegerField(primary_key=True)
    user = models.ForeignKey(User)
    title = models.CharField(max_length=300, blank=True) #文章标题
    link = models.CharField(max_length=200,blank=True) #链接
    content = models.TextField(blank=True) #文章内容
    category = models.CharField(max_length=30,blank=False) #文章分类
    video = models.TextField(blank=True) #视频地址，视频功能延后实现
    imgs = models.TextField(blank=True) #图片，以逗号分隔的多个图片地址，先保存图片并把地址存入此字段
    down = models.IntegerField()
    up = models.IntegerField()
    integral = models.IntegerField() #积分
    date = models.DateTimeField()

    class Meta:
        db_table = u'article'

class ArticleForm(ModelForm):

    class Meta:
        model = Article
        exclude = ('id','date')

class Comment(models.Model): #评论
    id = models.IntegerField(primary_key=True)
    article = models.ForeignKey(Article)
    content = models.TextField() #评论内容
    date = models.DateTimeField() #评论日期
    user = models.ForeignKey(User)

    class Meta:
        db_table = u'comment'

class Category(models.Model): #分类

    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=19,decimal_places=10) #价格

    class Meta:
        db_table = u'category'

class CategoryIntegral(models.Model): #分类积分

    id = models.IntegerField(primary_key=True)
    category_id = models.ForeignKey(Category)
    user = models.ForeignKey(User)
    year = models.CharField(max_length=10) #年
    week = models.CharField(max_length=10) #周
    integral = models.IntegerField() #积分

    class Meta:
        db_table = u'category_integral'

class Ad(models.Model): #广告
    id = models.IntegerField(primary_key=True)
    category = models.ForeignKey(Category)  #分类即位置，按分类时间不可有冲突
    start_date = models.DateField()
    end_date = models.DateField()
    date = models.DateTimeField() #添加日期
    total_price = models.DecimalField(max_digits=19, decimal_places=10) #广告总价

    class Meta:
        db_table = u'ad'

class Report(models.Model): #举报

    id = models.IntegerField(primary_key=True)
    article = models.ForeignKey(Article)
    user = models.ForeignKey(User)
    date = models.DateTimeField()

    class Meta:
        db_table = u'report'

class Friends(models.Model): #朋友

    id = models.IntegerField(primary_key=True)
    user = models.ForeignKey(User,related_name='user')
    follower = models.ForeignKey(User,related_name='follower') #关注者
    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = u'friends'


class UserProfile(models.Model): #用户扩展表，默认用django后果管理表作为用户表

    id = models.IntegerField(primary_key=True)
    user = models.OneToOneField(User)
    paypal = models.CharField(max_length=50,blank=True) #paypal帐号
    role = models.CharField(max_length=10,blank=True) #用户角色，高级会员，普通会员
    start_date = models.DateField(blank=True, null=True) #高级会员开始时间
    end_dete = models.DateField(blank=True, null=True) #高级会员结束时间
    medal = models.CharField(max_length=50,blank=True) #奖章
    avatar = models.ImageField(upload_to='photos/%Y/%m/%d')
    activation_key = models.CharField(max_length=50,blank=True)
    country = models.CharField(max_length=50,blank=True)
    state = models.CharField(max_length=50,blank=True)
    intro = models.CharField(max_length=200,blank=True)


    class Meta:
        db_table = u'user_profile'

class UserProfileForm(ModelForm):

    class Meta:
        model = UserProfile
        fields = ('paypal','avatar')

class UserForm(ModelForm):
    password=forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'size': 20,}))
    class Meta:
        model = User
        fields = ('username','email','password')

class LoginForm(ModelForm):
    username=forms.CharField(max_length=30,widget=forms.TextInput(attrs={'size': 20,}))
    password=forms.CharField(max_length=30,widget=forms.PasswordInput(attrs={'size': 20,}))
    class Meta:
        model = User
        fields = ('username','password')

class ResetPassForm(forms.Form):
    email = forms.EmailField(max_length='20')
    class Meta:
        pass

